#include "../../include/paths/Paths.h"
#include <filesystem>

namespace Circle {

    // 类外定义静态成员变量
    std::string Paths::__ROOT__;
    std::string Paths::__CONFIG__;
    std::string Paths::__LANG__;
    std::string Paths::__LOG__;
    std::string Paths::__MODULES__;

    std::string Paths::findProjectRoot() {
        const std::string targetDirName = "modules";
        std::filesystem::path currentPath = std::filesystem::current_path();
        while (true) {
            if (std::filesystem::exists(currentPath / targetDirName) && std::filesystem::is_directory(currentPath / targetDirName)) {
                return currentPath.string();
            }
            if (currentPath.parent_path() == currentPath) {
                break;
            }
            currentPath = currentPath.parent_path();
        }
        return "";
    }

    std::string Paths::getRoot() {
        static std::string root = findProjectRoot();
        return root;
    }



    // 实现静态初始化块的构造函数
    Paths::Initializer::Initializer() {
        __ROOT__ = getRoot();
        __CONFIG__ = __ROOT__ + "/config";
        __LANG__ = __ROOT__ + "/lang";
        __LOG__ = __ROOT__ + "/logs";
        __MODULES__ = __ROOT__ + "/modules";
    }

    // 初始化静态初始化块
    Paths::Initializer Paths::initializer;
}